<?php

class IntegralController extends AppController {

	public $name = 'integral';
	
	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array('index','add','edit','del');

	}	
	
	public function index() {
		$query = array(
			'order' => ' int_grade asc ', 
			'fields' => array('integral.*'), 
		);
		$integral = POP::exe('[@@:UM integral][@@:QU find:all]',$query);				
        $this->set('integral',$integral);				
	}	

	public function add() {
		if (!empty($this->data)) {
			$query = array(
				'conditions' => array('integral.int_grade' => $this->data['int_grade']), 
				'fields' => array('integral.*'), 
			);
			$row = POP::exe('[@@:UM integral][@@:QU find:first]',$query);			
			if($row) {			    
				$this->flash('"您不能重复添加该等级"', Router::url('admin/integral/index/'));	
			}else {
                Loader::import('lib.cupload');		
	            $up = new upload('jpg|gif|png|jpge',1024, 'credit_img');
	            $up->set_dir(APP_ROOT . Param::read('upfiles.path') . '/credit/','{y}/{m}/{d}');
	            $file = $up->execute();	
			    $rootpath = trim(str_replace(Param::read('wwwrootPatch'),"",Param::read('htdocPatch')),'/') ;
                $rootpath = !empty($rootpath)?	('/'.$rootpath) : '';				
                if($file) {
                    $realtxt=$file[0];
		            $this->data['int_img'] =  $rootpath. str_replace(APP_ROOT,"",$realtxt['dir']).$realtxt['name'];				
                }
                $safeData['integral'] = $this->data;
				$query = $safeData;
				if (POP::exe('[@@:UM integral][@@:QU save]',$query)) {				
					$this->flash('更新成功！', Router::url('admin/integral/index'));		
				} else {
					$this->flash('更新失败！', Router::url('admin/integral/index'));
				}			    
            }			
	    }	
	}	
	
	public function edit($id) {
		if (!empty($this->data)) {
			$query = array(
				'conditions' => array('integral.int_grade' => $this->data['int_grade']), 
				'fields' => array('integral.*'), 
			);
			$row = POP::exe('[@@:UM integral][@@:QU find:first]',$query);			
			if($row && $row['integral']['id'] != $id) {			    
				$this->flash('"您不能重复添加该等级"', Router::url('admin/integral/index/'));	
			}else {
                Loader::import('lib.cupload');		
	            $up = new upload('jpg|gif|png|jpge',1024, 'credit_img');
	            $up->set_dir(APP_ROOT . Param::read('upfiles.path') . '/credit/','{y}/{m}/{d}');
	            $file = $up->execute();	
			    $rootpath = trim(str_replace(Param::read('wwwrootPatch'),"",Param::read('htdocPatch')),'/') ;
                $rootpath = !empty($rootpath)?	('/'.$rootpath) : '';				
                if($file) {
                    $realtxt=$file[0];
		            $this->data['int_img'] =  $rootpath. str_replace(APP_ROOT,"",$realtxt['dir']).$realtxt['name'];				
                }
				$query['keyid'] = $id;
                $safeData['integral'] = $this->data;				
				$query['set'] = $safeData;			
	            if(POP::exe('[@@:UM integral][@@:QU save]',$query)) {					
					$this->flash('更新成功！', Router::url('admin/integral/index'));		
				} else {
					$this->flash('更新失败！', Router::url('admin/integral/index'));
				}			    
            }		 	
	    } else {
			$query = array(
				'conditions' => array('integral.id' => $id), 
				'fields' => array('integral.*'), 
			);
			$row = POP::exe('[@@:UM integral][@@:QU find:first]',$query);			
			$row = $row['integral'];			
            $this->set('row',$row);				
        }	
	}

	public function del($id) {
		$query = array('keyid'=>$id);			
        POP::exe('[@@:UM integral][@@:QU del]',$query);
		$this->flash('删除成功！', Router::url('admin/integral/index/'));		
	}
	
}
?> 